#include "InputManager.h"
#include <algorithm>
InputManager InputManager::mInputManager;


/** @brief AddKeyListener
  *
  * @todo: document this function
  */
void InputManager::AddKeyListener(KeyListener* listener)
{
    mKeyListeners.push_back(listener);

}

/** @brief RemoveKeyListener
  *
  * @todo: document this function
  */
void InputManager::RemoveKeyListener(KeyListener* listener)
{
    std::list<KeyListener*>::iterator it =  std::find(mKeyListeners.begin(),mKeyListeners.end(),listener);
    mKeyListenersIterator = mKeyListeners.erase(it);
}

/** @brief AddKeyListener
  *
  * @todo: document this function
  */
void InputManager::AddMouseListener(MouseListener* listener)
{
    mMouseListeners.push_back(listener);

}

/** @brief RemoveKeyListener
  *
  * @todo: document this function
  */
void InputManager::RemoveMouseListener(MouseListener* listener)
{
    std::list<MouseListener*>::iterator it =  std::find(mMouseListeners.begin(),mMouseListeners.end(),listener);
    mMouseListenersIterator = mMouseListeners.erase(it);
}

/** @brief ~InputManager
  *
  * @todo: document this function
  */
 InputManager::~InputManager()
{

}

/** @brief GetInstancePtr
  *
  * @todo: document this function
  */
InputManager * InputManager::GetInstancePtr()
{
    return &mInputManager;
}

/** @brief GetInstance
  *
  * @todo: document this function
  */
InputManager & InputManager::GetInstance()
{
    return mInputManager;
}

/** @brief InputManager
  *
  * @todo: document this function
  */
 InputManager::InputManager()
{

}

/** @brief Capture
  *
  * @todo: document this function
  */
bool InputManager::Capture()
{
    bool mContinue = true;
    while (SDL_PollEvent(&event))
    {
        switch (event.type)
        {
            case SDL_KEYDOWN:
            {
                mKeyListenersIterator = mKeyListeners.begin();
                for(;mKeyListenersIterator!=mKeyListeners.end();++mKeyListenersIterator)
                {
                    mContinue = mContinue==false?false:(*mKeyListenersIterator)->OnKeyDown(event.key.keysym.sym);
                }
                break;
            }
            case SDL_KEYUP:
            {
                mKeyListenersIterator = mKeyListeners.begin();
                for(;mKeyListenersIterator!=mKeyListeners.end();++mKeyListenersIterator)
                {
                    mContinue = mContinue==false?false:(*mKeyListenersIterator)->OnKeyUp(event.key.keysym.sym);
                }
                break;
            }
            case SDL_MOUSEMOTION:
            {
                mMouseListenersIterator = mMouseListeners.begin();
                for(;mMouseListenersIterator!=mMouseListeners.end();++mMouseListenersIterator)
                {
                    mContinue = mContinue==false?false:(*mMouseListenersIterator)->OnMouseMove(event.motion);
                }
                break;
            }
            case SDL_MOUSEBUTTONDOWN:
            {
                mMouseListenersIterator = mMouseListeners.begin();
                for(;mMouseListenersIterator!=mMouseListeners.end();++mMouseListenersIterator)
                {
                    mContinue = mContinue==false?false:(*mMouseListenersIterator)->OnMouseButtonDown(event.button);
                }
                break;
            }
            case SDL_MOUSEBUTTONUP:
            {
                mMouseListenersIterator = mMouseListeners.begin();
                for(;mMouseListenersIterator!=mMouseListeners.end();++mMouseListenersIterator)
                {
                    mContinue = mContinue==false?false:(*mMouseListenersIterator)->OnMouseButtonUp(event.button);
                }
                break;
            }
        }
    }
    return mContinue;
}

